package com.tr.rentcar.model.dao;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.tr.rentcar.model.GlobalParamHandler;
import com.tr.rentcar.model.bean.Employee;
import com.tr.rentcar.model.jdbc.MySqlDb;
import com.tr.rentcar.model.jdbc.ResultHandler;

public class EmployeeDao implements ResultHandler<Employee> {
	private MySqlDb db = MySqlDb.getInstance();

	/**
	 * 查询全部员工
	 * @return
	 * @throws SQLException
	 */
	public List<Employee> findAll() throws SQLException {
		String sql = "select * from employee";
		return db.executeQuery(sql, this);
	}
	
	/**
	 * 按电话姓名模糊查询
	 * @param tel
	 * @return
	 * @throws SQLException 
	 */
	public List<Employee> findByTel(String tel,String tel2) throws SQLException{
		String sql = "select * from employee where etel like ? or ename like ?";
		return db.executeQuery(sql, new GlobalParamHandler("%"+tel+"%","%"+tel2+"%"), this);
	}

	/**
	 * 查询空闲状态的员工
	 * 
	 * @return
	 * @throws SQLException
	 */
	public List<Employee> getAllByFree() throws SQLException {
		String sql = "select * from employee where estatus='空闲中'";
		return db.executeQuery(sql, this);
	}

	/**
	 * 根据id查找司机
	 * 
	 * @param tel
	 * @return
	 * @throws SQLException
	 */

	public Employee findById(Integer eid) throws SQLException {
		String sql = "select * from employee where eid = ?";
		return db.queryUninResult(sql, new GlobalParamHandler(eid), this);
	}

	/**
	 * 模糊查询司机信息
	 * 
	 * @param val
	 * @return
	 * @throws SQLException
	 */

	public List<Employee> findNameByVal(String val) throws SQLException {
		String sql = " SELECT * FROM employee WHERE ename LIKE ?";
		return db.executeQuery(sql,new GlobalParamHandler("%"+val+"%"), this);
	}

	// 保存一条记录
	public boolean save(String ename, String eidentity, String etel, Date ebir, String estatus, String ephoto)
			throws SQLException {
		String sql = "insert into employee(ename,eidentity,etel,ebir,estatus,ephoto) values(?,?,?,?,?,?)";
		return db.executeUpdate(sql, new GlobalParamHandler(ename, eidentity, etel, ebir, estatus, ephoto)) > 0;
	}

	// 通过id删除
	public boolean datelebyid(int id) throws SQLException {
		String sql = "delete from employee where eid=?";
		return db.executeUpdate(sql, new GlobalParamHandler(id)) > 0;
	}
	
	/**
	 * 根据id修改员工信息
	 * @param id
	 * @return
	 * @throws SQLException 
	 */
	public boolean updateAll(int id,String name,String identity,String tel,Date bir,String status) throws SQLException{
		String sql="update employee set ename=?,eidentity=?,etel=?,ebir=?,estatus=? where eid=?";
		return db.executeUpdate(sql, new GlobalParamHandler(name,identity,tel,bir,status,id)) > 0;
	}

	/**
	 * 修改员工状态
	 * 
	 * @param name
	 * @return
	 * @throws SQLException
	 */
	public boolean updateStatus(String estatus,String name) throws SQLException {
		String sql = "update employee set estatus=? where ename=?";
		return db.executeUpdate(sql, new GlobalParamHandler(estatus,name)) > 0;
	}
	@Override
	public Employee doHander(Map<String, Object> row) {
		Employee Employee = new Employee();
		Employee.setEid(Integer.parseInt(row.get("eid") + ""));
		Employee.setEname(row.get("ename") + "");
		Employee.setEidentity(row.get("eidentity") + "");
		Employee.setEtel(row.get("etel") + "");
		Employee.setEbir((Date) row.get("ebir"));
		Employee.setEditor(row.get("editor") + "");
		Employee.setEstatus(row.get("estatus") + "");
		Employee.setEphoto(row.get("ephoto") + "");
		return Employee;
	}

}
